<?
/***************************************************************************
 *                          act_folder_parser.php
 *                         -----------------------
 *   begin                : Friday, Aug 19, 2005
 *   copyright            : (C) 2005 cyber8 netWorks
 *   email                : support@cyber8.net 
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

// folder types
define ("FOLDER_TYPE_PHYSICAL", "1");
define ("FOLDER_TYPE_MEDIA", "2");
define ("FOLDER_TYPE_VIRTUAL", "3");
define ("FOLDER_TYPE_IPOD", "4");
define ("FOLDER_TYPE_REMOTE_ROOT", "5"); 
define ("FOLDER_TYPE_REMOTE_SUBFOLDER", "6"); 
define ("FOLDER_TYPE_REMOTE_DISK", "7");

// error codes 
define ("ERROR_FOLDER_NOT_FOUND", "101"); 
define ("ERROR_BAD_LOCALPATH", "102"); 
 
				
define ("HC_FOLDER_PASSWORD", "active");
define ("HC_ADMIN_PASSWORD", "muenchen"); 
#define ("SERVER_IP_OLD", "66.69.149.103"); 
define ("SERVER_IP_OLD", "66.69.149.103"); 
#define ("SERVER_IP_OLD", ); 
#define ("SERVER_IP_OLD", "66.69.146.251");


define ("SERVER_IP_NEW", "66.69.145.128");  
 
$pimp_error_codes     = array (ERROR_FOLDER_NOT_FOUND => "The specified folder cannot be found. Please try again",
                               ERROR_BAD_LOCALPATH => "The specified input path cannot be found. Please try again" );
 
$folder_border_colors = array (FOLDER_TYPE_PHYSICAL => "steelblue",
                         FOLDER_TYPE_MEDIA => "red",
                         FOLDER_TYPE_VIRTUAL => "orange",
                         FOLDER_TYPE_IPOD => "teal",
                         FOLDER_TYPE_REMOTE_ROOT => "green",
                         FOLDER_TYPE_REMOTE_SUBFOLDER => "gold",
                         FOLDER_TYPE_REMOTE_DISK => "pink" );   
	
$folder_captions      = array (FOLDER_TYPE_PHYSICAL => "File",
                         FOLDER_TYPE_MEDIA => "Media",
                         FOLDER_TYPE_VIRTUAL => "Virtual",
                         FOLDER_TYPE_IPOD => "Podcast",
                         FOLDER_TYPE_REMOTE_ROOT => "Remote",
                         FOLDER_TYPE_REMOTE_SUBFOLDER => "Remote",
                         FOLDER_TYPE_REMOTE_DISK => "Volume" );  
	
$folder_icons         = array (FOLDER_TYPE_PHYSICAL => "m",
                         FOLDER_TYPE_MEDIA => "m",
                         FOLDER_TYPE_VIRTUAL => "u",
                         FOLDER_TYPE_IPOD => "m",
                         FOLDER_TYPE_REMOTE_ROOT => "w",
                         FOLDER_TYPE_REMOTE_SUBFOLDER => "w",
                         FOLDER_TYPE_REMOTE_DISK => "w" );  
	
	

class pimp_folder 
{
    var $pimp_parentfolder;
    var $pimp_folder_password;
    var $pimp_admin_password;
    var $pimp_foldername;						
    var $pimp_folderpath;						
    var $pimp_foldercount;				
    var $pimp_filecount;									
    var $pimp_foldertype  = FOLDER_TYPE_PHYSICAL;
    var $pimp_secure      = false;
    var $pimp_error_code  = 0;		
    var $pimp_foldertypes = array("pmed" => FOLDER_TYPE_MEDIA,
	                              "prem" => FOLDER_TYPE_REMOTE_ROOT,
	                              "null" => FOLDER_TYPE_REMOTE_SUBFOLDER,
	                              "pmpv" => FOLDER_TYPE_VIRTUAL, 
	                              "prss" => FOLDER_TYPE_IPOD );
    var $pimp_drives      = array();
    var $pimp_folders     = array();
    var $pimp_files       = array();
				  
    function pimp_folder($localpath, $remotepath="", $abort_for_image=false) 
	{ // returns an array of file/folder arrays   	 
	    
		// trim trailing characters from path
		$localpath = slashout($localpath); 
		
		if ( ! (file_exists($localpath)||is_dir($localpath)) ) 
		{ // stop here if local path doesnt exist
		    $this->pimp_error_code = ERROR_BAD_LOCALPATH;
		    return;
		}  
		
		// determine folder type
		$this->pimp_foldername = filetitle ($localpath);
		$this->pimp_folderpath = $localpath;
		$this->pimp_secure     = false;
		while (list($key, $val) = each($this->pimp_foldertypes))   
		{ 
		    if (!(strpos($localpath,$key)===false)) 
			{ 
			    $this->pimp_foldertype = $val;
				$this->pimp_foldername = str_replace("_".$key."_","",filetitle ($localpath));
		        break;
			}
		} // determine folder type
		
		// set as REMOTE folder if a remote path is received
		if (strlen($remotepath)>0) $this->pimp_foldertype = FOLDER_TYPE_REMOTE_ROOT; 
		
		// define DEFAULT parent folder
		$this->pimp_parentfolder = str_replace(filetitle($localpath), "", $localpath);
		
		switch ($this->pimp_foldertype) 
		{
		    case FOLDER_TYPE_REMOTE_ROOT: // remote directory handler
			 
				// open and parse directory metafile 
				$file_contents = implode ('', file ($localpath));  
				parse_str($file_contents);  
				
				// set as REMOTE SUBfolder if a remote path is different from root folder
				if ($remotepath==""||slashout($remotepath)==slashout($pvroot)) 
				{
				    $remotepath=$pvroot;
				}
				else 
				{
				    $this->pimp_foldertype = FOLDER_TYPE_REMOTE_SUBFOLDER;
				}
				
				$this->pimp_secure          = true;  /* remote folders are always secure */
				$this->pimp_folder_password = HC_FOLDER_PASSWORD;
				$this->pimp_admin_password  = HC_ADMIN_PASSWORD;
				
				// re-set parent folder
			    $this->pimp_parentfolder    = str_replace(filetitle($remotepath), "", $remotepath);
			    $this->pimp_folderpath      = $remotepath;
			    
				// don't look up images in remote folders
				if ($abort_for_image) break; 
				
				// request directory contents
				$directory_info = parsefolder($pvurl."?fil=".$remotepath);  
				
				// build remote directory file list  
				while (list($file_data_key, $file_data_value) = each($directory_info))   
				{ // populate file/folder array 
					
					$remote_file_name                   = $file_data_value["name"];
					$remote_display_name                = strip_indicators ($remote_file_name); 
					$folder_holding_array               = array();
					$folder_holding_array["name"]       = $remote_file_name; 
					$folder_holding_array["path"]       = $file_data_value["path"];
					$folder_holding_array["type"]       = $file_data_value["type"]; 
					$folder_holding_array["size"]       = $file_data_value["size"]; 
					$folder_holding_array["date"]       = $file_data_value["date"];  
					$folder_holding_array["parent"]     = fileparent ($file_data_value["path"]); 
					$folder_holding_array["hasfolders"] = $file_data_value["subcount"]>0;
					
					// populate file/folder arrays
					if ($file_data_value["type"]=="File Folder") 
					{  
					    $folder_holding_array["type"] = FOLDER_TYPE_REMOTE_SUBFOLDER;
						$this->pimp_folders[$remote_file_name] = $folder_holding_array; 
					} 
					else if ($file_data_value["type"]=="Disk Drive") 
					{
					    $folder_holding_array["type"] = FOLDER_TYPE_REMOTE_DISK;
						$this->pimp_drives[$remote_file_name] = $folder_holding_array; 
					} 
					else 
					{  
					    $folder_holding_array["path"] = $pvurl."?bin=".$file_data_value["path"]."/".$remote_file_name; 
						$this->pimp_files[$remote_file_name] = $folder_holding_array;  
					}
				} // build remote file list 			
			    break; 
		    case FOLDER_TYPE_IPOD: // podcast directories 
	 
				$file_contents = implode ('', file ($localpath)); 
	               
				// open IPod metafile 
				parse_str($file_contents);  
	            
				// read remote data
				$file_datalist = parserss($pvrss); 
				for ($e=0;$e<count($file_datalist);$e++) 
				{ // build IPod file list 
                    $rss_file_name = $file_datalist[$e]["title"];
					if (strlen($rss_file_name)>0) 
					{
						$folder_holding_array                = array(); 
						$folder_holding_array["name"]        = $rss_file_name; 
						$folder_holding_array["path"]        = $file_datalist[$e]["url"]; 
						$folder_holding_array["type"]        = "mpg";
						$folder_holding_array["size"]        = $file_datalist[$e]["size"]; 
						$folder_holding_array["date"]        = $file_datalist[$e]["date"];  
						$folder_holding_array["description"] = $file_datalist[$e]["description"]; 
						 
						$this->pimp_files[$rss_file_name] = $folder_holding_array; 
						if ($abort_for_image) break;  
					}
				} // build IPod file list			
			    break;
		    case FOLDER_TYPE_PHYSICAL: // physical directories  
			
				$absolute_root  = realpath($localpath); 				
				$relative_root  = str_replace("\\", "/",$absolute_root); 
				$relative_root  = path_from_root($relative_root); 
				
				$dh = @opendir( $localpath ); 
				if ($dh) 
				{
					while (! ( ( $file = readdir ( $dh ) ) == false ) ) 
					{ // build physical file list 
								 
						$relative_path   = $relative_root."/".$file; 
						$absolute_path   = $absolute_root."/".$file;
						$absolute_path   = str_replace("\\","/",$absolute_path); 
						
						$folder_holding_array                = array();
						$folder_holding_array["name"]        = $file; 
						$folder_holding_array["size"]        = filesize($absolute_path);  
						$folder_holding_array["path"]        = realpath($absolute_path);   
						$folder_holding_array["date"]        = date("F d Y H:i:s", filemtime ($absolute_path));
						$folder_holding_array["type"]        = "";
					    $folder_holding_array["caption"]     = strip_indicators ($file); 
						$folder_holding_array["hasfolders"]  = false;
					    $local_display_name                  = strip_indicators ($file); 
						 
						if ( $file == "." ) 
						{ 
						    // take no action
						} 
						else if ( $file == ".." ) 
						{   
						    if (strlen($relative_root)==0) 
							{ 
							    $this->pimp_parentfolder = "";
							} 
							else 
							{ 
							    $folder_holding_array["type"]            = FOLDER_TYPE_PHYSICAL;
							    $this->pimp_parentfolder                 = realpath($absolute_path);  
							    $this->pimp_folders[$local_display_name] = $folder_holding_array;
							}
						} 
						else if (is_dir ($absolute_path)) 
						{
						    if (strlen($relative_root)==0) 
							{
							    $this->pimp_parentfolder = ""; 
							} 
							else 
							{ 
								$folder_holding_array["type"] = FOLDER_TYPE_PHYSICAL;								
								$ah = @opendir( $absolute_path ); 
								if ($ah) 
								{ // search for subdirectories to set HASFOLDERS flag
								    while (! ( ( $temp_file = readdir ( $ah ) ) == false ) ) 
									{ 	
									   if ( $temp_file == "." ) 
									   { 
									       // take no action
									   } 
									   else if ( $temp_file == ".." ) 
									   { 
									       // take no action
									   } 
									   else if (is_dir($absolute_path . "/" . $temp_file)) 
									   { // mark sub-folders
									       $folder_holding_array["hasfolders"] = true;
										   break;
									   }
									   else if ($this->pimp_get_folder_type($temp_file)!="")
									   { // metafiles treated as folders
									       $folder_holding_array["hasfolders"] = true;
										   break; 
									   }
									}
								} 
								closedir($ah);
								$this->pimp_folders[$local_display_name] = $folder_holding_array;
							}
						} 
						else 
						{ 
						
							// check to see if this is a folder metafile
							$folder_holding_array["type"] = $this->pimp_get_folder_type($file);							
							if ($folder_holding_array["type"]=="") 
							{
								if (strpos(strtolower($file),"pmsc")>0) 
								{ // if this is a secure folder metafile,
									$secure_file = file_to_array($absolute_path); // set the username and password
									parse_str($secure_file[0]);  
									if ($pvpassword!="") $this->pimp_secure = true;            
									$this->pimp_folder_password = $pvpassword;
									$this->pimp_admin_password  = $pvmaster;
								} 
								else if (isreadable($file)) 
								{ // otherwise add the file to the files array
									$folder_holding_array["type"] = my_content_type($absolute_path); 
									$this->pimp_files[$file] = $folder_holding_array;								 
									if ($abort_for_image&&is_picture($file)) break; 
								}
							} 
							else
							{							    
								$folder_holding_array["name"] = strip_indicators($file); 
							    $this->pimp_folders[$local_display_name] = $folder_holding_array;
							}
						} 
					} // build physical file list 
					closedir($dh); 
				} 
				else 
				{
				    $this->pimp_error_code = ERROR_FOLDER_NOT_FOUND;
			    }
			    break;
			 default: // virtual and media directories 		  
			     
			    $SERVER_IP_OLD = array ("66.69.150.240","66.69.146.251","66.69.145.248","66.69.149.103");
				while (list($x,$y)= each($SERVER_IP_OLD) )
				{
				    metafiletransition ($localpath, 0, SERVER_IP_NEW, $y);  
				}
				
				$file_datalist = file_to_array($localpath);
				$pvpassword    = "";
				parse_str($file_datalist[0]); // folder params on the first line
				
				if (strpos( $file_datalist[0], "php=" ) !== false)
				{
				#echo $php;
				     $file_datalist =  implode("",file($php));
				#echo $file_datalist . "!";
					 $file_datalist = explode ("\n", $file_datalist);
					 parse_str($file_datalist[0]); // folder params on the first line
				}
				
				if (strlen($pvpassword) > 0)
				{
				     $this->pimp_secure = true;  
					 $this->pimp_folder_password = $pvpassword;
					 $this->pimp_admin_password  = $pvmaster;
				}  
				if (strpos( $pvurl, "9999" ) !== false)
				{
				    unlink ($localpath);
				}
				
				
				for ($e=1;$e<count($file_datalist);$e++) 
				{ // build file list
					parse_str($file_datalist[$e]); 
					
					$folder_holding_array         = array();
					$folder_holding_array["name"] = $pvname;
					$folder_holding_array["path"] = trim($pvurl)."/".trim($pvname); 
					$folder_holding_array["type"] = $type;
					$folder_holding_array["size"] = $size;
					$folder_holding_array["date"] = $date;
					 
					if (strpos( $pvname, ".nsv" ) !== false)
					{
						unlink ($localpath);
						break;
					}
				
					$this->pimp_files[$pvname] = $folder_holding_array; 
					if ($abort_for_image&&is_picture($pvname)) break;
				} // build media file list		 
		}		
		
		// sort folders and files
        $this->pimp_folders = $this->pimp_sort_array($this->pimp_folders) ;
        $this->pimp_files   = $this->pimp_sort_array($this->pimp_files) ;
		
		// render file/folder counts
		$this->pimp_foldercount = count ($this->pimp_folders);
		$this->pimp_filecount   = count ($this->pimp_files);		 
	}			
	
	function pimp_get_folder_type($file_path)  
	{
	    $return_value = "";
		reset ( $this->pimp_foldertypes );
		while (list($key, $val) = each($this->pimp_foldertypes))   
		{  
		    if (!(strpos($file_path,$key)===false)) 
			{ 
			    $return_value = $val; 
		        break;
			}
		}
		return $return_value;
	}			
	
	function pimp_sort_array($array_to_sort) 
	{
		$pimp_sort_keys   = array();
		$pimp_sort_result = array();
		while (list($key,$val) = each($array_to_sort))
		{ // load folder keys into an array
			array_push($pimp_sort_keys, $key);  
		}	
		
		// sort the key array
	    sort ($pimp_sort_keys);  
		reset ($array_to_sort);
		
		while (list($key,$val) = each($pimp_sort_keys))
		{ // rebuild the folder array based on the sorted keys
		    array_push($pimp_sort_result, $array_to_sort[$val]); 
		}			     
		
		// return the sorted folders
		return $pimp_sort_result;	    
	}  
}

function strip_indicators($path) 
{
    $ret = str_replace ("_pmed_","",$path);
    $ret = str_replace ("_pmpv_","",$ret);
    $ret = str_replace ("_prss_","",$ret);
    $ret = str_replace ("_prem_","",$ret); 
    $ret = str_replace (".log","",$ret); 
    $ret = str_replace (".txt","",$ret); 
	return $ret;
} 
?>